//
//  MsgBoxLayer.h
//  airfight
//
//  Created by lahm on 12-12-2.
//  Copyright (c) 2012年 moonriver. All rights reserved.
//

#include "MsgBoxLayer.h"

MsgBoxLayer::MsgBoxLayer()
: m_bTouchedMenu(false)
{
    
}

MsgBoxLayer::~MsgBoxLayer()
{
    
}

bool MsgBoxLayer::init()
{
    do
    {
        CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(0, 0, 0, 125)));
    }while (0);
    
    return true;
}

void MsgBoxLayer::initDialog(char *text, SEL_MenuHandler okHandler, SEL_MenuHandler cancelHandler)
{
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    
    CCSprite *back = CCSprite::create();
    back->initWithSpriteFrameName("msgbox.png");
    back->setPosition(ccp(winSize.width / 2, winSize.height / 2 + 50));
    addChild(back);
    
    CCLabelTTF *labelText = CCLabelTTF::labelWithString(text, CCSize(200, 150), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter, "", 15);
    labelText->setPosition(ccp(winSize.width / 2, winSize.height / 2 + 70));
    addChild(labelText);
    
    CCSprite *buttonOk = CCSprite::create();
    buttonOk->initWithSpriteFrameName("button.png");
    CCSprite *buttonOkClicked = CCSprite::create();
    buttonOkClicked->initWithSpriteFrameName("button.png");
    buttonOkClicked->setScale(0.95);
    CCMenuItemSprite *okMenuItem = CCMenuItemSprite::itemWithNormalSprite(buttonOk, buttonOkClicked, buttonOk, this, okHandler);
    okMenuItem->setPosition(ccp(winSize.width / 2 - 70, winSize.height / 2 - 10));
    CCLabelTTF *labelBtOk = CCLabelTTF::labelWithString("确定", CCSize(50, 30), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter, "", 15);
    labelBtOk->setPosition(ccp(winSize.width / 2 - 60, winSize.height / 2 - 8));
    
    CCSprite *buttonCancel = CCSprite::create();
    buttonCancel->initWithSpriteFrameName("button.png");
    CCSprite *buttonCancelClicked = CCSprite::create();
    buttonCancelClicked->initWithSpriteFrameName("button.png");
    buttonCancelClicked->setScale(0.95);
    CCMenuItemSprite *cancelMenuItem = NULL;
    if (cancelHandler)
    {
        cancelMenuItem = CCMenuItemSprite::itemWithNormalSprite(buttonCancel, buttonCancelClicked, buttonCancel, this, cancelHandler);
    }
    else
    {
        cancelMenuItem = CCMenuItemSprite::itemWithNormalSprite(buttonCancel, buttonCancelClicked, buttonCancel, this, menu_selector(MsgBoxLayer::clickCancel));
    }
    cancelMenuItem->setPosition(ccp(winSize.width / 2 + 70, winSize.height / 2 - 10));
    CCLabelTTF *labelBtCancel = CCLabelTTF::labelWithString("取消", CCSize(50, 30), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter, "", 15);
    labelBtCancel->setPosition(ccp(winSize.width / 2 + 80, winSize.height / 2 - 8));
    
    m_pMenu = CCMenu::menuWithItems(okMenuItem, cancelMenuItem, NULL);
    m_pMenu->setPosition(CCPointZero);
    addChild(m_pMenu);
    addChild(labelBtOk);
    addChild(labelBtCancel);
}

void MsgBoxLayer::onEnter()
{
    CCLayerColor::onEnter();
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority - 1, true);
    scheduleUpdate();
}

void MsgBoxLayer::onExit()
{
    CCLayerColor::onExit();
    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}

bool MsgBoxLayer::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
    m_bTouchedMenu = m_pMenu->ccTouchBegan(pTouch, pEvent);
    return true;
}

void MsgBoxLayer::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
    if (m_bTouchedMenu)
    {
        m_pMenu->ccTouchMoved(pTouch, pEvent);
    }
}

void MsgBoxLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
    if (m_bTouchedMenu)
    {
        m_pMenu->ccTouchEnded(pTouch, pEvent);
        m_bTouchedMenu = false;
    }
}

void MsgBoxLayer::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
    if (m_bTouchedMenu)
    {
        m_pMenu->ccTouchEnded(pTouch, pEvent);
        m_bTouchedMenu = false;
    }
}

void MsgBoxLayer::clickCancel()
{
    removeFromParentAndCleanup(true);
}

